/**
  ******************************************************************************
  * @file    PWM.h 
  * @author  Dajin Li
  * @version V0.0.1
  * @date    08-08-2015 
  * @brief   This file contains headers for pwm function
  *				 
  *          
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; KION(SZ) R&D Center
  *
  * Licensed under KION, (the "License");
  *
  ******************************************************************************
  */

#ifndef __PWM_H_
#define __PWM_H_

#include "stm32f10x.h"
#include "global.h"
#include "pwm.h"

#define BKLIGHT_MIN 		          1
#define BKLIGHT_DEFAULT	              5
#define BKLIGHT_MAX  		          240    // 24000

#define PWM_CAPTURE_MAX               512

#define PWM_GET_TH                    0
#define PWM_GET_TH1                   1
#define PWM_GET_TV                    2
#define PWM_GET_TK                    3
#define PWM_GET_TK1                   4
#define PWM_GET_TR                    5

#define PWM_CALCULATE                 6

#define MAX_FIND_CNTS                 16

#define PWM_OFFSET                    50      // 100khz, +- 0.5ms.  

#define PWM_TV_MAX                    9000    // 100khz, 90ms, 
#define PWM_TV_MIN                    500     // 100khz, 5ms,

#define PWM_TR_MAX                    12000   // 100khz, 120ms,
#define PWM_TR_MIN                    2000    // 100khz, 20ms,

#define PWM_TH_MAX                    5000    // 100khz, 100ms,
#define PWM_TH_MIN                    100     // 100khz, 2ms,
 
#define PWM_TK_MAX                    100000  // 100khz, 1000ms                              
#define PWM_TK_MIN                    15000   // 100khz, 150ms

#define PWM_K1_CONST                  90930   // 909.3ms *100
#define PWM_S1_CONST                  1220    // 12.2ms * 100

#define PWM_K2_CONST                  83200   // 832ms *100
#define PWM_S2_CONST                  750     // 7.5ms * 100

#define PWM_TR_CONST                  10000   // 100ms

#define ENG_TEMP1_LENGTH              10       // This is the number of points in table.
#define ENG_TEMP2_LENGTH              20       // This is the number of points in table.


#define HZ_TO_RPM                     1254     // 60/49*1024, Gear tooth:49, Axle ratio(gear/wheel):14.2, tyre radius:0.327
#define RPM_TO_KM                     142      // 1/14.2*pi*0.327*60/1000*1024*16

#define HZ_TO_KM_1319                 4343/500       //  1024 0.265
#define HZ_TO_KM_1219                 11       //  1024 

#define PWM_MAX_HZ                    3000     // Maximum pwm frequency   

typedef union
{

    struct
    {
        short	Precision;
        short	Output;
    } Data;

    long int Sum;

} FilterType;



extern void pwm_duty_adjust(int duty);
extern void lcd_adjust_lighting(int bk_light_level, U8 bk_status);
extern u32 pwm_period_value;
extern s32 traction_speed_raw1_temp;
extern s32 traction_speed_raw_temp;
extern s32 traction_speed_raw_hz;
extern s32 traction_speed_raw_hz_slewed;
extern s32 traction_speed_raw;
extern s16 eng_oil_level;
extern s16 eng_oil_temp;
extern void ADSample_Timer3_Init(void);
extern void LCD_PWM_Init(void);
#endif

